<template>
  <el-dialog
    :close-on-click-modal="false"
    title="批量修改"
    v-model="visible"
    align-center
    width="500"
    destroy-on-close
  >
    <el-form
      ref="formRef"
      :model="form"
      label-position="top"
      :label-width="140"
    >
      <el-form-item label="销售人员">
        <VCascader
          v-model="form.sale_id"
          placeholder="请选择人员"
          class="w-full"
        />
        <div class="text-[red]">
          注: 单个SKU多个销售人员,修改后会合并为一个销售人员
        </div>
      </el-form-item>
      <el-form-item label="运输方式">
        <VSelect
          v-model="form.transport_type"
          placeholder="运输方式"
          :options="options"
          class="w-full"
        />
      </el-form-item>
      <el-form-item label="需求日期">
        <el-date-picker
          v-model="form.demand_time"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择"
          class="w-full"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="handleClose(false)">取 消</el-button>
      <el-button type="primary" :loading="loading" @click="save"
        >保 存</el-button
      >
    </template>
  </el-dialog>
</template>

<script setup lang="jsx">
import { ElMessage } from "element-plus";
import api from '../api/index.js'
import {
  isObjectEmptyOrAllFieldsEmpty,
  removeEmptyFields,
} from "@/utils/index";
import { resMessage } from "@/utils/res";
const props = defineProps({
  options: { type: Array, default: () => [] },
});
const { options } = toRefs(props);
const emit = defineEmits(["close", "success"]);
const loading = ref(false);
const visible = ref(false);
const form = ref({});
// 打开弹窗
const pds_ids_data = ref([]);
const open = async (id) => {
  visible.value = true;
  pds_ids_data.value = id;
};
const handleClose = (refresh) => {
  visible.value = false;
  form.value = {};
  pds_ids_data.value = [];
  emit("close");
  refresh && emit("success");
};
const save = async () => {
  console.log(form.value);
  if (isObjectEmptyOrAllFieldsEmpty(form.value))
    return ElMessage.warning("没有修改数据!");
  let filteredForm = removeEmptyFields(form.value);
  filteredForm.pds_ids = pds_ids_data.value;
  loading.value = true;
  try {
    const res = await api.batch_edit(filteredForm);
    resMessage({
      res,
      success: () => {
        ElMessage.success("保存成功");
        handleClose(true);
      },
    });
  } finally {
    loading.value = false;
  }
};
defineExpose({ open });
</script>

<style></style>
>
